package cn.saltybe.analysis.crawler.wbtc.newHouse;

import cn.saltybe.analysis.crawler.wbtc.common.WbtcHouseImage;
import cn.saltybe.analysis.entity.data.NewHouse;
import cn.saltybe.analysis.service.NewHouseServiceImpl;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component("wbtcNewHousePipeline")
@PipelineName("wbtcNewHousePipeline")
public class WbtcNewHousePipeline implements Pipeline<WbtcNewHouse> {
    private static NewHouseServiceImpl newHouseService;

    @Autowired
    public void setNewHouseService(NewHouseServiceImpl newHouseService) {
        this.newHouseService = newHouseService;
    }

    @Override
    public void process(WbtcNewHouse wbtcNewHouse) {
        try {
            String typeId = "wbtc" + wbtcNewHouse.getId();

            HttpRequest currentRequest = wbtcNewHouse.getRequest();

            String province = currentRequest.getParameter("province");
            String city = currentRequest.getParameter("city");

            // 在这里进行抓取到信息的逻辑处理
            // System.out.println("Title: " + wbtcNewHouse.getTitle() + ", price: " + wbtcNewHouse.getPrice() + wbtcNewHouse.getUnit() + ", address: " + wbtcNewHouse.getAddress());
            // for (WbtcHouseImage i : wbtcNewHouse.getImage()) System.out.println("image: " + i.getAlt() + ", " + i.getImg());
            // 正则匹配区名
            String district = "未知";
            Pattern districtPattern = Pattern.compile("\\[(\\s*)(.*(区|县|周边))\\s*(.*?)\\]");
            Matcher districtMatcher = districtPattern.matcher(wbtcNewHouse.getAddress());
            if (districtMatcher.find()) {
                district = districtMatcher.group(2);
            }

            if (wbtcNewHouse.getLabel() != null && wbtcNewHouse.getLabel().equals("周边均价")) {
                // 周边均价不是该房产的真实价格，而是使用安居客大数据计算的。这个数据是无效的，不进行收集了
                return;
            } else if (wbtcNewHouse.getUnit() == null || !wbtcNewHouse.getUnit().equals("元/㎡")) {
                // 单位不是元/㎡的不是目标数据，不采集
                return;
            }
            NewHouse newHouse = new NewHouse();
            newHouse.setCity(city);
            newHouse.setDistrict(district);
            newHouse.setSource(currentRequest.getUrl());
            newHouse.setPrice(Integer.parseInt(wbtcNewHouse.getPrice()));
            newHouse.setTitle(wbtcNewHouse.getTitle());
            newHouse.setAddress(wbtcNewHouse.getAddress());
            for (WbtcHouseImage i : wbtcNewHouse.getImage()) {
                if (i.getImg().length() > 0) {
                    newHouse.setImage(i.getImg());
                    break;
                }
            }
            newHouse.setType_id("wbtc" + wbtcNewHouse.getId());
            try {
                if (newHouseService.has(typeId)) {
                    System.out.println("更新旧条目");
                    newHouseService.update(newHouse);
                    System.out.println("旧条目已更新");
                } else {
                    System.out.println("插入新条目");
                    newHouseService.insert(newHouse);
                    System.out.println("新条目已入库");
                }
                System.out.println(typeId + "\t" + province + "\t" + city + "\t" + district);
                System.out.println(currentRequest.getUrl());
                System.out.println(wbtcNewHouse.getTitle());
                System.out.println(wbtcNewHouse.getAddress());
                System.out.println(wbtcNewHouse.getPrice() + wbtcNewHouse.getUnit());
            } catch (Exception e) {
                System.out.println("条目置入数据库失败");
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
